//
//  GCW_SingerViewController.m
//  MooMusic
//
//  Created by 管晨雯 on 15/10/21.
//  Copyright © 2015年 wjy. All rights reserved.
//
#define TAG 99
#import "GCW_SingerViewController.h"
#import "GCW_SingerCollectionViewCell.h"
#import "PrefixHeader.pch"
#import "RGCardViewLayout.h"
#import "NetWork.h"
#import <UIImageView+WebCache.h>
#import "GCW_SingerModel.h"
#import "GCW_SingerListViewController.h"
@interface GCW_SingerViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *singerCollectionView;
@property (nonatomic, strong) NSMutableArray *imageArr;

@end

static NSString *const  cellIndetifier = @"cellIndetifier";
@implementation GCW_SingerViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.imageArr = [NSMutableArray array];
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"歌手";
    self.navigationController.navigationBar.translucent = NO;
    //    self.view.backgroundColor = [UIColor cyanColor];
    
    [self.view addSubview:self.singerCollectionView];
    self.singerCollectionView.delegate = self;
    self.singerCollectionView.dataSource = self;
    self.singerCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self creadDate];
    
    
}

#pragma mark --- 创建UICollectionView
- (UICollectionView *)singerCollectionView
{
    if (_singerCollectionView != nil) {
        return _singerCollectionView;
    }
    
    RGCardViewLayout *layout = [[RGCardViewLayout alloc] init];
    self.singerCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.singerCollectionView.pagingEnabled = YES;
    [self.singerCollectionView registerClass:[GCW_SingerCollectionViewCell class] forCellWithReuseIdentifier:cellIndetifier];
    return _singerCollectionView;
}

#pragma mark --- 获取数据
- (void)creadDate
{
    
    [NetWork networkGETRequestWithURL:singerPageUrl  Paramater:nil ResultBlock:^(id result) {
        
        NSMutableDictionary *mDic = (NSMutableDictionary *)result;
        
        NSMutableArray *dataArr = [mDic objectForKey:@"data"];
        
        for (NSMutableDictionary *dataDic in dataArr) {
            GCW_SingerModel *model = [[GCW_SingerModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [self.imageArr addObject:model];
            //            NSLog(@"%@",model.pic_url);
            
        }
        
        if (self.imageArr.count > 0) {
            [self.singerCollectionView reloadData];
        }
        
        
    }];
}



#pragma mark --- 实现协议的方法
  /* section的数量 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.imageArr.count;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GCW_SingerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndetifier forIndexPath:indexPath];
    
    //    [self configureCell:cell withIndexPath:indexPath];
    
    GCW_SingerModel *model = [self.imageArr objectAtIndex:indexPath.section];
    
    [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil];
    
    cell.typeLabel.text = model.title;
    
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.section);
    GCW_SingerModel *model = [self.imageArr objectAtIndex:indexPath.section];
    GCW_SingerListViewController *list = [[GCW_SingerListViewController alloc]init];
    list.idNumber = model.id;
    list.singerType = model.title;
    [self.navigationController pushViewController:list animated:YES];
    
    
//    UINavigationController *listNavi = [[UINavigationController alloc] initWithRootViewController:list];
//    listNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    
//    [self presentViewController:listNavi animated:YES completion:^{
//        
//    }];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
